﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using RSSReader.MobileClient.ViewModel;
using RSSReader.MobileClient.Model;

namespace RSSReader.MobileClient
{
    public partial class CreateUserPage : PhoneApplicationPage
    {
        private CreateUserViewModel CreUservm;

        public CreateUserPage()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            CreUservm = new CreateUserViewModel();
        }

        private void ValidateUserCreationB_Click(object sender, RoutedEventArgs e)
        {
            if (CreateUserOnPage.Username.Text != "" && CreateUserOnPage.Pwd.Text != "" && CreateUserOnPage.PwdConfirm.Text != "" && CreateUserOnPage.Email.Text != "")
            {
                if (CreateUserOnPage.Pwd.Text == CreateUserOnPage.PwdConfirm.Text)
                {
                    var asyncMgr = new AsyncDataManager(() =>
                    {
                        if (CreUservm.Usr.UserId != null)
                            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
                        else
                            MessageBox.Show("Username already used. Please modify username");
                    });
                    asyncMgr.SetExpectation("UserReached");
                    CreUservm.AddUser(CreateUserOnPage.Username.Text, CreateUserOnPage.Pwd.Text, CreateUserOnPage.Email.Text, asyncMgr);
                }
                else
                    MessageBox.Show("The password and its confirmation are different");
            }
            else
                MessageBox.Show("Please fill all the requested fields");
        }
    }
}